/*
 * Utils.cpp
 *
 *  Created on: Aug 5, 2013
 *      Author: alexandru
 */

#include "Utils.h"

namespace stn {

Utils::Utils() {}

Utils::~Utils() {}


std::string Utils::dts(double x){
			std::stringstream s;
			s << x;
			return s.str();
}


std::string Utils::safetizeCommand(std::string str){
	std::string result;
	for (unsigned i=0; i<str.length(); ++i){
					if(!Utils::IsWhitespace(str.at(i))){
						result+=str.at(i);
					}
				}
	return result;
}



//std::string Utils::trim(std::string str)
//{
//	str.erase(0, str.find_first_not_of(' '));       //prefixing spaces
//	str.erase(str.find_last_not_of(' ')+1);         //surfixing spaces
//	return str;
//}


std::vector<SimpleStruct> Utils::convertToSimpleStruct(std::string input){

	using namespace std;
	std::vector<SimpleStruct> response;

	if(input==UTILS_STR_NULL || input==UTILS_STR_UNDEFINED){ // daca e null sau undefined
		return response;
	}


	bool first=true;
	bool allow=true;
	SimpleStruct obi;

	bool strStart=false;
	for(int i=0; i< (int) input.length(); i++){
		char c=input.at(i);

		switch(c){


			case '"':
				if(strStart) strStart=true;
				else strStart=false;

			break;
//				case '\n':
//				case '\t':

//				case '\r':
//				case '\f':
				case NEWLINE:
//				case std::endl;
				case ' ':


					if(!strStart){
						first=true;
						boost::algorithm::trim(obi.property);
						boost::algorithm::trim(obi.value);

						if(obi.property.length()>0 && obi.value.length()>0){
							boost::algorithm::trim(obi.property);
							boost::algorithm::trim(obi.value);

							response.push_back(obi);
						}


						obi.property.clear();
						obi.value.clear();
					}

				break;

				case '=':
					first=false;
					allow=false;
				break;

				default:
					allow=true;
				break;
		};


		if(allow){
			if(first){
				obi.property+=c;
			}else{
				obi.value+=c;
			}
		}



	}
	boost::algorithm::trim(obi.property);
	boost::algorithm::trim(obi.value);

	if(obi.property.length()>0 && obi.value.length()>0){
		boost::algorithm::trim(obi.property);
		boost::algorithm::trim(obi.value);
		response.push_back(obi);
	}
	return response;
}

inline bool Utils::IsWhitespace(char c){
			return (c == '\n' || c == ' ' || c == '\t' || c == '\r' || c == '\f');
		}

bool Utils::isAllEmpty(std::string str){

	int num=0;
	for (unsigned i=0; i<str.length(); ++i){
		if(Utils::IsWhitespace(str.at(i))){
			num++;
		}
	}
	if(num==(int)str.length()){
		return true;
	}

	return false;
}



bool Utils::isLikeWildcard(std::string value, std::string wildcard){
	using namespace std;

	boost::algorithm::trim(wildcard);

	string::size_type loc;
	std::string keyword;


	char frst=wildcard.at(0);
	char lst=*wildcard.rbegin();
	//*prop
	if(frst=='*' && lst !='*'){
		keyword=wildcard.erase(0, 1);
		loc=value.find(keyword, 0);
		cout << "PRIMUL "<<frst << "   ULTIMUL: " << lst<< " locatia lui "<<keyword<<" in "<< value << " este "<< loc << " lungine value:"<< value.length()<<endl;

		return (loc==value.length()-keyword.length());
	}

	//prop*
	else if(frst!='*' && lst =='*'){
			keyword=wildcard.erase(wildcard.size()-1);
			loc=value.find(keyword, 0);
			cout << "PRIMUL "<<frst << "   ULTIMUL: " << lst<< " locatia lui "<<keyword<<" in "<< value << " este "<< loc << " lungine value:"<< value.length()<<endl;
			return (loc==0);
		}

	else if(frst=='*' && lst =='*'){
		wildcard.erase(0, 1);//scoatem primul
		keyword=wildcard.erase(wildcard.size()-1);//scoatem ultimul
		cout << "PRIMUL "<<frst << "   ULTIMUL: " << lst<< " locatia lui "<<keyword<<" in "<< value << " este "<< loc << " lungine value:"<< value.length()<<endl;
		loc=value.find(keyword);
		return (loc!=string::npos);
	}

	else{
		cout << "COMPARAM DIRECT" << endl;
		return (value==wildcard);
	}


return false;
}

} /* namespace stn */
